//
//  fact.c
//  Algorithm
//
//  Created by ViliWen on 1/19/16.
//  Copyright © 2016 wlf92. All rights reserved.
//

#include "fact.h"

// recursion
int fact(int n)
{
    if (n < 0)
        return 0;
    else if (n == 0)
        return 1;
    else if (n == 1)
        return 1;
    else
        return n * fact(n-1);
}

// tail recursion
int facttail(int n, int a)
{
    if (n < 0)
        return 0;
    else if (n == 0)
        return 1;
    else if (n == 1)
        return a;
    else
        return facttail(n-1, n*a);
}